package com.ruoyi.traces.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.traces.domain.TraceTask;
import com.ruoyi.traces.domain.TraceUserMaterial;
import com.ruoyi.traces.domain.TraceUserProduct;
import com.ruoyi.traces.domain.vo.MaterialVo;
import com.ruoyi.traces.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/traces/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    /**
     * 查询用户产品总数
     */
    @GetMapping("/count")
    public int count()
    {
        List<MaterialVo> list = productService.selectProductList();
        return list.size();
    }

    @GetMapping("/list")
    public TableDataInfo queryProductList(){
        startPage();
        List<MaterialVo> list = productService.selectProductList();
        return getDataTable(list);
    }
    @GetMapping("/queryProductList/{deptId}")
    public TableDataInfo queryProductList(@PathVariable Integer deptId){
        LoginUser loginuser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginuser.getUser();
        startPage();
        List<MaterialVo> list = new ArrayList<MaterialVo>();
        if(SecurityUtils.isAdmin(sysUser.getUserId())){
            list = productService.selectProductList();
        }
        else{
            list = productService.queryProductList(deptId);
        }
        return getDataTable(list);
    }

    @PostMapping("/addTask")
    public AjaxResult addTask(@RequestBody TraceTask traceTask){
        return AjaxResult.success(productService.addTask(traceTask));
    }

    @PostMapping("/updateMachingStatus")
    public AjaxResult updateMachingStatus(@RequestBody MaterialVo materialVo){
        return AjaxResult.success(productService.updateMachingStatus(materialVo));
    }

    @GetMapping("/queryTaskByCropsId/{materialId}")
    public AjaxResult queryTaskByCropsId(@PathVariable("materialId") String materialId){
        return AjaxResult.success(productService.queryTaskByCropsId(materialId));
    }

    @GetMapping("/updateProductWriteStatus/{cropsId}")
    public AjaxResult updateProductWriteStatus(@PathVariable("cropsId") String cropsId){
        return AjaxResult.success(productService.updateProductWriteStatus(cropsId));
    }

    @GetMapping("/queryTaskList")
    public AjaxResult queryTaskList(){
        return AjaxResult.success(productService.queryTaskList());
    }

    @GetMapping("/productOutFactory/{materialId}")
    public AjaxResult productOutFactory(@PathVariable("materialId") String materialId){
        return AjaxResult.success(productService.productOutFactory(materialId));
    }
    @PostMapping("/updateProgress")
    public AjaxResult updateProgress(@RequestBody TraceTask traceTask){
        return AjaxResult.success(productService.updateProgress(traceTask));
    }
}
